package com.roc.service.impl;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.roc.bean.TblMgmRiskCodeBean;
import com.roc.dao.DaoException;
import com.roc.dao.IBaseDao;
import com.roc.dao.ISQLBaseDao;
import com.roc.newPage.IPageDao;
import com.roc.pojo.Operators;
import com.roc.service.ReturnCodeService;
import com.roc.service.ServiceException;
import com.roc.util.Constance;
import com.roc.util.constant.SysLogType;

@Service
@Transactional
public class ReturnCodeServiceImpl implements ReturnCodeService {
static Logger logger=Logger.getLogger(ReturnCodeServiceImpl.class);
	
	@Autowired
	private IPageDao pageDao;
	@Autowired
	private ISQLBaseDao sqlBaseDao;
	
	@Autowired
	private IBaseDao baseDao;
	/**
	 * 添加证件返回码实现
	 */
	@Override
	public int addReturnCode(String explan, Operators op)throws ServiceException {
		String sql=" UPDATE apstfr.tbl_mgm_risk_code  a  SET  a.remark1='1'   WHERE  a.RETURN_EXPLAN=? ";
		int effect=0;
		List<Object> parmas=new ArrayList<Object>();
		parmas.add(explan);				
		try {
			effect=sqlBaseDao.deleteBySql(sql, parmas);
			if(effect>0)
			{
				logger.info("添加高危返回码【"+explan+"】成功!");
				baseDao.addSyslog(SysLogType.ADD_RISK_RETURN_CODE.getCode(), SysLogType.ADD_RISK_RETURN_CODE.getName(), op.getAccountNo(), "操作员【" + op.getAccountNo()+"添加高危返回码【"+explan+"】成功");
			}
		} catch (DaoException e) {
			logger.debug(e.getMessage(),e);
			throw new ServiceException("添加高危返回码失败");
		}
		return effect;
	}

	/**
	 * 删除证件返回码实现
	 */
	@Override
	public int deleteReturnCode(String value, Operators op) throws ServiceException {
		String sql=" UPDATE apstfr.tbl_mgm_risk_code  a  SET  a.remark1='0'   WHERE  a.return_value=? ";
		int effect=0;
		List<Object> parmas=new ArrayList<Object>();
		parmas.add(value);				
		try {
			effect=sqlBaseDao.deleteBySql(sql, parmas);
			if(effect>0)
			{
				logger.info("删除高危返回码【"+value+"】成功!");	
				baseDao.addSyslog(SysLogType.UPDATE_RISK_RETURN_CODE.getCode(), SysLogType.UPDATE_RISK_RETURN_CODE.getName(), op.getAccountNo(), "操作员【" + op.getAccountNo()
						+ "】+删除高危返回码【"+value+"】成功");
			}
		} catch (DaoException e) {
			logger.debug(e.getMessage(),e);
			throw new ServiceException("删除高危返回码失败！！！");
			
		}
		return effect;
	}

	@Override
	public List<TblMgmRiskCodeBean> queryreturnExplan() throws ServiceException {

	List<TblMgmRiskCodeBean> returnCodeList = new ArrayList<TblMgmRiskCodeBean>();
		
		
		try {
			//Object[] params = {"0" };
			String sql_1=" SELECT T.*  FROM apstfr.tbl_mgm_risk_code  T WHERE  T.REMARK1='0' ";
			List<?> lists = sqlBaseDao.queryAll(sql_1);
			
          if(null != lists && lists.size() > 0){
        	  Iterator iterator=lists.iterator();
        	  while (iterator.hasNext()) {
					Object[] obj=(Object[]) iterator.next();
					TblMgmRiskCodeBean exp=new TblMgmRiskCodeBean();
					exp.setValue(Constance.Null2EmptyTrim(obj[0]));
					exp.setExplan(Constance.Null2EmptyTrim(obj[1]));
					returnCodeList.add(exp);
			}
			}
		} catch (Exception e) {
			e.printStackTrace();
			logger.error("加载高危返回码失败", e);
		}	
		return returnCodeList;
	}
	@Override
	public List<TblMgmRiskCodeBean> queryNoReturnExplan()throws ServiceException {
        List<TblMgmRiskCodeBean> returnCodeList = new ArrayList<TblMgmRiskCodeBean>();
		try {
			String sql_1=" SELECT T.*  FROM apstfr.tbl_mgm_risk_code  T WHERE  T.REMARK1='1' ";
			List<?> lists = sqlBaseDao.queryAll(sql_1);
			
          if(null != lists && lists.size() > 0){
        	  Iterator iterator=lists.iterator();
        	  while (iterator.hasNext()) {
					Object[] obj=(Object[]) iterator.next();
					TblMgmRiskCodeBean exp=new TblMgmRiskCodeBean();
					exp.setValue(Constance.Null2EmptyTrim(obj[0]));
					exp.setExplan(Constance.Null2EmptyTrim(obj[1]));
					returnCodeList.add(exp);
			}
			}
		} catch (Exception e) {
			e.printStackTrace();
			logger.error("加载高危返回码失败", e);
		}	
		return returnCodeList;
	}


}
